package com.weifeng.learneasyexcel.learnpoi;


import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;

/***
 * 注意读取的时候 不同类型读取方式不一样
 */

public class Read {
    public static void main(String[] args) throws IOException {
        String file07 = "C:\\Users\\Administrator\\Desktop\\07版.xlsx";
//        read07one(file07);
        read07bigdata(file07);
    }
    public static void read07one(String file07) throws IOException{
        FileInputStream fileInputStream = new FileInputStream(file07);

        Workbook sheets = new XSSFWorkbook(fileInputStream);
        Sheet sheetAt = sheets.getSheetAt(0);
        String cell = sheetAt.getRow(0).getCell(0).getStringCellValue();
        System.out.println(cell);
        fileInputStream.close();

    }

    public static void read07bigdata(String file07) throws IOException{
        FileInputStream fileInputStream = new FileInputStream(file07);

        Workbook sheets = new XSSFWorkbook(fileInputStream);
        Sheet sheet = sheets.getSheetAt(0);
        int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();

        Row row1 = sheet.getRow(0);
        int cellnumber = row1.getPhysicalNumberOfCells();
        System.out.println("一共 ："+ physicalNumberOfRows+" 行，"+ cellnumber+"列");

        for (int i = 0; i < physicalNumberOfRows; i++) {
            Row row = sheet.getRow(i);
            int physicalNumberOfCells = row.getPhysicalNumberOfCells();
//            System.out.println("一共 ： "+physicalNumberOfRows+"行, "+physicalNumberOfCells+"列。");
            for (int j = 0; j < physicalNumberOfCells; j++) {
                Cell cell = row.getCell(j);
                if (cell != null){
                    switch (cell.getCellType()){
                        case STRING:
                            System.out.print(cell.getStringCellValue()+"  ");
                            break;
                        case NUMERIC:
                            System.out.print(cell.getNumericCellValue()+"  ");
                            break;
                        case BOOLEAN:
                            System.out.print(cell.getBooleanCellValue()+"  ");
                            break;
                        case FORMULA:
                            System.out.print(cell.getArrayFormulaRange()+"  ");
                            break;
                    }
                    if (j == physicalNumberOfCells-1){
                        System.out.println(" ");
                    }
                }

            }
            fileInputStream.close();
        }

    }
}
